package pe.com.certicom.ittsa.control.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import pe.com.certicom.ittsa.control.form.MapaForm;
import pe.com.certicom.ittsa.framework.exception.DaoException;
import pe.com.certicom.ittsa.model.bean.BeanBusMapa;
import pe.com.certicom.ittsa.model.bean.BeanGeoCode;
import pe.com.certicom.ittsa.model.bo.DispAsientoBO;
import pe.com.certicom.ittsa.model.constants.ValueConstants;
import pe.com.certicom.ittsa.model.dao.MapaDao;

public class MostrarMapaAction extends Action {
	private static final Log log = LogFactory.getLog(MostrarMapaAction.class);
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward actionforward = null;
		HttpSession session = request.getSession(true);
		int method = Integer.parseInt(request.getParameter("method"));

		try {
			log.info("Inicio MostrarMapaAction");
			log.debug("Method : " + method);
			switch (method) {
				case 10:
				actionforward = inicio(mapping, form, request, response,
						session);
				break;
				case 11:
					actionforward = irMenuUbicacionBus(mapping, form, request, response, session);
					break;
				case 12:
					actionforward = irCargarDestino(mapping, form, request, response, session);
					break;
				case 13:
					actionforward = irMostrarUbicacion(mapping, form, request, response, session);
					break;
				case 14:
					actionforward = irMostrarMapa(mapping, form, request, response, session);
					break;
			}
		} catch (Exception e) {
			log.error("Error en Switch Case ... ", e);
		} finally {
			log.info("Inicio MostrarMapaAction");
		}
		return actionforward;
	}
	
	public ActionForward inicio(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws IOException, ServletException,
			DaoException {
		MapaForm formulario = (MapaForm) form;

		return mapping.findForward(ValueConstants.MENU_INICIO);
	}
	
	private ActionForward irMostrarMapa(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) {
		log.info("Inicio irMostrarMapa");
		String codbus = request.getParameter("codbus");
		String coord_x_act = request.getParameter("coord_x_act");
		String coord_y_act = request.getParameter("coord_y_act");
		String coord_x = request.getParameter("coord_x");
		String coord_y = request.getParameter("coord_y");
		String grados = request.getParameter("grados");
		String origendestino = request.getParameter("origendestino");
		String servicio = request.getParameter("servicio");
		String fecha = request.getParameter("fecha");
		String hora = request.getParameter("hora");
		String ubicacion = request.getParameter("ubicacion");
		String tiempo = request.getParameter("tiempo");
				
		log.debug("Datos recibidos:");
		log.debug("codbus: " + codbus);
		log.debug("coord_x_act: " + coord_x_act);
		log.debug("coord_y_act: " + coord_y_act);
		log.debug("coord_x: " + coord_x);
		log.debug("coord_y: " + coord_y);
		log.debug("grados: " + grados);
		log.debug("origendestino: " + origendestino);
		log.debug("servicio: " + servicio);
		log.debug("fecha: " + fecha);
		log.debug("hora: " + hora);
		log.debug("ubicacion: " + ubicacion);
		log.debug("tiempo: " + tiempo);
		
		request.setAttribute("codbus", codbus);
		request.setAttribute("coord_x_act", coord_x_act);
		request.setAttribute("coord_y_act", coord_y_act);
		request.setAttribute("coord_x", coord_x);
		request.setAttribute("coord_y", coord_y);
		request.setAttribute("grados", grados);
		request.setAttribute("origendestino", origendestino);
		request.setAttribute("servicio", servicio);
		request.setAttribute("fecha", fecha);
		request.setAttribute("hora", hora);
		request.setAttribute("ubicacion", ubicacion);
		request.setAttribute("tiempo", tiempo);
		
		log.info("Fin irMostrarMapa");
		return mapping.findForward("mapa");
	}



	public ActionForward irCargarDestino(ActionMapping mapping,	ActionForm form, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException, ServletException, Exception {
		log.info("Inicio irCargarDestino");
		MapaForm formulario = (MapaForm) form;
		BeanGeoCode departamento = formulario.getDepartamento();
		Map map = new HashMap();
		List listaDestino = new ArrayList();
		listaDestino = DispAsientoBO.obtenerInstancia().listarDestino(departamento.getIdDepartamento());
		map.put(ValueConstants.LISTA_DESTINO, listaDestino);
		formulario.setMapDestino(map);
		if( formulario.getDepartamento().getFecha()!=null && !formulario.getDepartamento().getFecha().equals("")) {
			request.setAttribute("fecha",formulario.getDepartamento().getFecha().trim());
		}
		log.info("Fin irCargarDestino");
		return mapping.findForward(ValueConstants.MENU_UBIC_BUS);
	}
		
	public ActionForward irMenuUbicacionBus(ActionMapping mapping, ActionForm form, HttpServletRequest request,	HttpServletResponse response, HttpSession session) throws IOException, ServletException, Exception {
		log.info("Inicio irMenuUbicacionBus");
		MapaForm formulario = (MapaForm) form;

		Map map = new HashMap();
		List listaOrigen = new ArrayList();
		List listaDestino = new ArrayList();
		listaOrigen = DispAsientoBO.obtenerInstancia().listarOrigen();

		map.put(ValueConstants.LISTA_ORIGEN, listaOrigen);
		map.put(ValueConstants.LISTA_DESTINO, listaDestino);

		formulario.setMapOrigen(map);
		if(formulario.getMapDestino()==null){
		formulario.setMapDestino(map);
		}
		//formulario.setDepartamento(new BeanGeoCode());
		log.info("Fin irMenuUbicacionBus");
		return mapping.findForward(ValueConstants.MENU_UBIC_BUS);
	 }

	public ActionForward irMostrarUbicacion(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, HttpSession session){		
		MapaForm formulario = (MapaForm) form;
		BeanGeoCode departamento = formulario.getDepartamento();

		ArrayList lugares = new ArrayList();
		
		try {
			log.info("Inicio irMostrarUbicacion");
			lugares = (ArrayList) DispAsientoBO.obtenerInstancia().listarOrigen();

			for (Object a : lugares) {
				if (departamento.getIdDepartamento() == ((BeanGeoCode) (a)).getIdDepartamento()) {
					departamento.setDepartamentoOrigen(((BeanGeoCode) (a)).getDepartamento());
				}
			}
			List<BeanBusMapa> arrBuses = MapaDao.obtenerBuses(departamento.getDepartamentoOrigen(), departamento.getDepartamento(), departamento.getFecha());
			request.setAttribute("arrBuses", arrBuses);
			
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			log.error("DaoException ... ", e);
			request.setAttribute("message", e.getMessage());
			return mapping.findForward("error");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("Exception ... ", e);
			request.setAttribute("message", e.getMessage());
			return mapping.findForward("error");
		}
		finally{
			log.info("Fin irMostrarUbicacion");
		}
		
		return mapping.findForward("success");
			 	
	}	
}
